package com.neuedu.controller;

import com.github.pagehelper.PageInfo;
import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.OrderService;
import com.neuedu.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/order/")
@Api(value = "订单相关接口",description = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @ApiOperation(value = "创建订单",notes = "创建订单",httpMethod = "POST")
    @RequestMapping(value = "createOrder",method = RequestMethod.POST)
    @ResponseBody
    public JSONData createOrder(HttpSession session,Integer shippingId){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        //2、创建订单
        JSONData<OrderVo> data = orderService.createOrder(userInfo.getId(), shippingId);
        return data;
    }
    @ApiOperation(value = "订单详情",notes = "订单详情",httpMethod = "POST")
    @RequestMapping(value = "orderDetail",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<OrderVo> orderDetail(HttpSession session,Long orderNo){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        JSONData<OrderVo> data = orderService.orderDetail(userInfo.getId(), orderNo);
        return data;
    }
    @ApiOperation(value = "订单列表",notes = "订单列表",httpMethod = "POST")
    @RequestMapping(value = "orderList",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<PageInfo<OrderVo>> orderList(HttpSession session,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        JSONData<PageInfo<OrderVo>> data = orderService.orderList(userInfo.getId(), pageNum, pageSize);
        return data;
    }

    /**
     * 取消订单：考虑问题
     *      1、是否是订单
     *          不考虑：=》订单是否已经支付（前端处理：订单已支付，就没有取消按钮）
     *      2、修改订单的状态
     *      3、商品对应的库存需要增加
     */
    @ApiOperation(value = "取消订单",notes = "取消订单",httpMethod = "POST")
    @RequestMapping(value = "cancel",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> cancel(HttpSession session,Long orderNo){
        //1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo==null){
            return JSONData.buildError("您未登录，请先登录");
        }
        JSONData<String> data = orderService.cancel(userInfo.getId(), orderNo);
        return data;
    }
}
